﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Localizer
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Localizer by Scott Ketelaar.\r\nPlaces/updates the appropriate localization files after build.");
            Environment.CurrentDirectory = args[0];
            bool move = bool.Parse(args[1]);
            Console.WriteLine("CWD: " + Environment.CurrentDirectory);
            Console.WriteLine("Move: " + move);
            Console.ForegroundColor = ConsoleColor.Gray;
            UpdateLangFiles(move);
        }

        static void UpdateLangFiles(bool move)
        {
            int errors = 0;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\r\nBegun updating lang files");
            System.IO.Directory.CreateDirectory("Localization\\Langs");

            //string[] dirs = Directory.GetDirectories(Environment.CurrentDirectory, "??"); //Depreciated

            string[] dirs = new string[] { "zh-Hant", "zh-Hans" };
            foreach (string dir in dirs)
            {
                string rPath = Path.GetFileName(dir);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write(rPath + "\t");
                string destDir = "Localization\\Langs\\" + rPath;
                Directory.CreateDirectory(destDir);
                if (!Directory.Exists(destDir))
                {
                    try
                    {
                        Directory.CreateDirectory(destDir);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("Created\t");
                    }
                    catch (Exception ex) { errors++; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Creation failed: " + ex.Message); continue; }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("Exists\t");
                }
                DirectoryInfo ds, df;
                ds = new DirectoryInfo(dir); df =  new DirectoryInfo(destDir);
                if (ds.LastWriteTime > df.LastWriteTime)
                {
                    try
                    {
                        CopyFilesRecursively(ds, df);
                    }
                    catch (Exception ex)
                    {
                        errors++;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Copy failed: " + ex.Message);
                        continue;
                    }
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("Copied\t");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("No change\t");
                }
                if (move)
                {
                    try
                    {
                        Directory.Delete(dir, true);
                    }
                    catch (Exception e)
                    {
                        errors++;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Clean failed: " + e.Message);
                        continue;
                    }
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Source removed");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Source left");
                }
            }
            if (errors != 0)
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("{0} errors occured. Review the errors, then press any key . . .", errors);
                Console.ReadKey(true);
            }
        }

        public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
        {
            foreach (DirectoryInfo dir in source.GetDirectories())
                CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
            foreach (FileInfo file in source.GetFiles())
            {
                string dst = Path.Combine(target.FullName, file.Name);
                if (File.Exists(dst))
                    File.Delete(dst);
                file.CopyTo(dst);

            }

        }


        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\r\n\r\n===U N H A N D L E D  E X C E P T I O N===");
            Console.WriteLine(((Exception)e.ExceptionObject).ToString());
            Console.WriteLine();
            Console.WriteLine("Is exiting: {0}",e.IsTerminating);
            Console.ForegroundColor = ConsoleColor.Gray;
            if (e.IsTerminating)
            {
                Console.WriteLine("Press any key to exit . . .");
                Console.ReadKey(true);
                Environment.Exit(1);
            }
        }
    }
}
